package Exercise;
//生成随机验证码
import java.util.Random;
public class String_10 {
    public static void main(String[] args) {
        //生成XXXX5类似格式的验证码
        String sr = Works();
        //打乱顺序
        System.out.println(DuLuan(sr));
    }
    //生成指定格式的字符串
    public static String Works(){
        StringBuilder sb = new StringBuilder();
        //生成随机数
        Random rd = new Random();
        char c;
        int num = rd.nextInt(10);  //数字
        //生成字母
        for (int i = 0; i < 4;) {
            int zimu = rd.nextInt(123);
            //当出现大小写字母时，才添加到容器中
            if(zimu>=65&&zimu<=90||zimu>=97) {
                c = (char) zimu;
                sb.append(c);
                i++;
            }
        }
        String sr = sb.append(num).toString();
        return sr;
    }
    //打乱
    public static String DuLuan(String sr){
        Random rd = new Random();
        //将字符串转换为字符数组
        char[] cr = sr.toCharArray();
        //交换打乱
        for (int i = 0; i < cr.length; i++) {
            //产生随机索引
            int flag = rd.nextInt(5);
            char c = cr[flag];
            cr[flag] = cr[i];
            cr[i] = c;
        }
        String srs = new String(cr);  //将字符数组变为字符串返回
        return srs;
    }
}
